package com.mqtt.web.controller.iot;

import com.mqtt.MqttConsumer;
import com.mqtt.common.core.domain.AjaxResult;
import com.mqtt.system.domain.IOTData;
import com.mqtt.system.domain.vo.IOTDataPage;
import com.mqtt.system.domain.vo.IOTDataPageResult;
import com.mqtt.system.domain.vo.UpdateRecords;
import com.mqtt.system.service.impl.IOTDataServiceImpl;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wang
 * @version 1.0
 * @data 2024/3/26 下午 8:09
 * @desc 描述信息
 */
@RestController
@RequestMapping("/iot")
public class IOT {

    @Autowired
    private IOTDataServiceImpl iotDataService;

    @GetMapping("/list")
    public AjaxResult getList(@RequestParam String p
            , @RequestParam(required = false, defaultValue = "1") Long page
            , @RequestParam(required = false, defaultValue = "10") Long pageSize
    ) {
        return AjaxResult.success(iotDataService.getListByProperties(p, page, pageSize));
    }

    @PostMapping("/sendCmd")
    public AjaxResult sendCmd(@RequestBody Map<String, String> map) {
        String cmd = map.get("cmd");
        String code = "esp32/command";
        MqttConsumer.publish(code, cmd);
        return AjaxResult.success("下发成功");
    }

    @GetMapping("/getData")
    public AjaxResult getData(@RequestParam String p) {
        System.out.println(p);
        IOTData oneData = iotDataService.getOneData(p);
        return AjaxResult.success(oneData);
    }

    @GetMapping("/query-device-property")
    public AjaxResult getProperty() {
        List<IOTData> iotData = iotDataService.queryProperty();
        return AjaxResult.success("成功", iotData);
    }

    @GetMapping("/device-history")
    public AjaxResult getHistory(@RequestParam Long page, @RequestParam Long pageSize) {
        IOTDataPageResult iotData = iotDataService.pageList(page, pageSize);
        return AjaxResult.success("成功", iotData);
    }

    @PostMapping("/delete")
    public AjaxResult deleteRecords(@RequestBody Map<String,List<Long>> map) {
        List<Long> ids = map.get("ids");
        Boolean aBoolean = iotDataService.deleteRecords(ids);
        return aBoolean ? AjaxResult.success("操作成功"):AjaxResult.error("操作失败");
    }

    @PostMapping("/updates")
    public AjaxResult updateRecords(@RequestBody Map<String,List<IOTData>> map) {
        List<IOTData> records = map.get("records");
        Boolean aBoolean = iotDataService.updateRecords(records);
        return aBoolean ? AjaxResult.success("操作成功"):AjaxResult.error("操作失败");
    }
    @PostMapping("/insertRecords")
    public AjaxResult inertRecords(@RequestBody Map<String,List<IOTData>> map){
        List<IOTData> records = map.get("records");
        Boolean aBoolean = iotDataService.inertRecords(records);
        return aBoolean ? AjaxResult.success("操作成功"):AjaxResult.error("操作失败");
    }

}
